package com.springboot.controller;

import com.springboot.mapper.DoctorAppointSetMapper;
import com.springboot.model.DoctorAppointSet;
import com.springboot.model.DoctorInfoForm;
import com.springboot.service.DoctorAppointSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 本控制台用于医生的可预约时间设置和修改
 * 传入的预约日期book_date参数必须是2020-11-03类型，不可以是2020-11-3
 */
@Controller
public class DoctorAppointSetController {
    @Autowired
    DoctorAppointSetService doctorAppointSetService;

    @Autowired
    DoctorAppointSetMapper doctorAppointSetMapper;

    /**
     * 医生设置可预约时间
     */
    @CrossOrigin
    @RequestMapping(value = "/updateAppointmentSetting")
    public @ResponseBody
    String doctorAppointSet(@RequestParam("doctorId") String tel,
                            @RequestParam("chooseDate") String date,
                            @RequestParam("morningNum") String morningNum,
                            @RequestParam("afternoonNum") String afternoonNum)
    {
        System.out.println("医生设置可预约时间");
        try {
            int morningNumber=Integer.valueOf(morningNum);
            int afternoonNumber=Integer.valueOf(afternoonNum);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
            DoctorInfoForm doctor = doctorAppointSetService.findDoctorByTel(tel);
            int doctorId = doctor.getDoctorId();
            System.out.println(doctorId);
            //根据id查找该医生的可预约时间表
            ArrayList<DoctorAppointSet> doctorSets = doctorAppointSetService.findDoctorAppointById(doctorId);
            if(doctorSets.size()==0)
            {
                DoctorAppointSet doctorAppointSet=new DoctorAppointSet();
                doctorAppointSet.setDoctorId(doctorId);
                doctorAppointSet.setBookDate(format.parse(date));
                doctorAppointSet.setMorningNum(morningNumber);
                doctorAppointSet.setAfternoonNum(afternoonNumber);
                doctorAppointSetService.insertDoctorAppoint(doctorAppointSet);
                return "True";
            }
            else
            {
                String []arrayDate=new String[doctorSets.size()+1];
                Boolean isExist=false;
                for (int i = 0; i < doctorSets.size(); i++)
                {
                    DoctorAppointSet doctorAppointSet=doctorSets.get(i);
                    String appointDate = format.format(doctorAppointSet.getBookDate());
                    arrayDate[i]=appointDate;
                }
                for (int i = 0; i < doctorSets.size(); i++)
                {
                    //如果该日期已经填过，则表示修改可预约时间
                    if (arrayDate[i].equals(date))
                    {
                        isExist=true;
                        break;
                    }
                }
                DoctorAppointSet doctorAppointSet=new DoctorAppointSet();
                //如果该日期已经填过，则表示修改可预约时间
                if(isExist)
                {
                    doctorAppointSet.setDoctorId(doctorId);
                    doctorAppointSet.setBookDate(format.parse(date));
                    doctorAppointSet.setMorningNum(morningNumber);
                    doctorAppointSet.setAfternoonNum(afternoonNumber);
                    doctorAppointSetService.updateDoctorSet(doctorAppointSet);
                    return "True";
                }
                //如果该日期没有填过，则表示设置可预约时间
                else
                {
                    doctorAppointSet.setDoctorId(doctorId);
                    doctorAppointSet.setBookDate(format.parse(date));
                    doctorAppointSet.setMorningNum(morningNumber);
                    doctorAppointSet.setAfternoonNum(afternoonNumber);
                    doctorAppointSetService.insertDoctorAppoint(doctorAppointSet);
                    return "True";
                }
            }
        }catch (Exception e)
        {
            return "False";
        }
    }

    /**
     * 管理员新增可预约时间
     */
    @CrossOrigin
    @RequestMapping("/adminAddAppoint")
    @ResponseBody
    public String addAppoint(@RequestBody DoctorAppointSet doctorAppointSet) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(doctorAppointSet.getBookDate());
        int doctorId = doctorAppointSetMapper.selectDoctorIdByName(doctorAppointSet.getDoctorName());
        doctorAppointSet.setDoctorId(doctorId);
        DoctorAppointSet doctorAppointSet1 = doctorAppointSetMapper.searchAppoint(doctorAppointSet.getDoctorId(), date);
        if(doctorAppointSet1==null){
            int i = doctorAppointSetService.insertDoctorAppoint(doctorAppointSet);
            if(i>0){
                System.out.println("success");
                return "success";
            }else {
                System.out.println("error");
                return "error";
            }
        }
        else {
            return "fail";
        }

    }

    /**
     * 管理员修改预约信息
     */
    @CrossOrigin
    @RequestMapping("/adminEditAppoint")
    @ResponseBody
    public String editAppoint(@RequestBody DoctorAppointSet doctorAppointSet){
        //姓名和预约日期框隐藏，联合主键
        Integer id = doctorAppointSetMapper.selectDoctorIdByName(doctorAppointSet.getDoctorName());
        doctorAppointSet.setDoctorId(id);
        int i = doctorAppointSetService.updateDoctorSet(doctorAppointSet);
        if(i>0)return "success";
        else return "error";
    }

    /**
     * 管理员删除预约记录
     */
    @CrossOrigin
    @RequestMapping("/deleteAppoint")
    @ResponseBody
    public String delete(@RequestParam("doctorId") Integer doctorId, @RequestParam("bookDate") String bookDate) throws ParseException {
        int i = doctorAppointSetMapper.deleteByPrimaryKey(doctorId,bookDate);
        if(i>0)return "success";
        else return "error";
    }
}